﻿#include "AlphaTestPass.h"

#include "../../../Common/Singleton.h"
#include "../ResourceManager/Dx2GeometryManager.h"
#include "../ResourceManager/Dx2MaterialManager.h"
#include "../ResourceManager/Dx2RootSignatureManager.h"
#include "../ResourceManager/Dx2ShaderAndLayoutManager.h"

void AlphaTestPass::BuildRenderItems(std::vector<std::unique_ptr<RenderItem>>& allRitems,UINT& lastObjIndex)
{
    auto boxRitem = std::make_unique<RenderItem>();
    XMStoreFloat4x4(&boxRitem->World, XMMatrixTranslation(3.0f, 2.0f, -9.0f));
    boxRitem->ObjCBIndex = lastObjIndex;
    lastObjIndex++;
    boxRitem->Mat = Singleton<Dx2MaterialManager>::Instance()->GetMaterial("wirefence").get();
    boxRitem->Geo = Singleton<Dx2GeometryManager>::Instance()->GetGeometry("boxGeo").get();
    boxRitem->PrimitiveType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
    boxRitem->IndexCount = boxRitem->Geo->DrawArgs["box"].IndexCount;
    boxRitem->StartIndexLocation = boxRitem->Geo->DrawArgs["box"].StartIndexLocation;
    boxRitem->BaseVertexLocation = boxRitem->Geo->DrawArgs["box"].BaseVertexLocation;

    mRenderItems.push_back(boxRitem.get());
    allRitems.push_back(move(boxRitem));
}

void AlphaTestPass::BuildPSOs(ID3D12Device* device, DXGI_FORMAT BackBufferFormat, DXGI_FORMAT DepthStencilFormat,
    bool _4xMsaaState, UINT _4xMsaaQuality)
{
    D3D12_GRAPHICS_PIPELINE_STATE_DESC alphaTestedPsoDesc;
    ZeroMemory(&alphaTestedPsoDesc,sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));
	
    alphaTestedPsoDesc.InputLayout = {Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetInputLayout().data(),(UINT)Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetInputLayout().size()};
    alphaTestedPsoDesc.pRootSignature = Singleton<Dx2RootSignatureManager>::Instance()->GetRootSignature(RootSignatureType::Default).Get();
	
    alphaTestedPsoDesc.VS =
        {
        reinterpret_cast<BYTE*>(Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("standardVS")->GetBufferPointer()),
        Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("standardVS")->GetBufferSize()
    };
	
    alphaTestedPsoDesc.PS =
        {
        reinterpret_cast<BYTE*>(Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("alphaTestedPS")->GetBufferPointer()),
        Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("alphaTestedPS")->GetBufferSize()
    };
	
    alphaTestedPsoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
    alphaTestedPsoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
    alphaTestedPsoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
    alphaTestedPsoDesc.SampleMask = UINT_MAX;
    alphaTestedPsoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
    alphaTestedPsoDesc.NumRenderTargets = 1;
    alphaTestedPsoDesc.RTVFormats[0] = BackBufferFormat;
    alphaTestedPsoDesc.SampleDesc.Count = _4xMsaaState? 4:1;
    alphaTestedPsoDesc.SampleDesc.Quality = _4xMsaaState? (_4xMsaaQuality - 1):0;
    alphaTestedPsoDesc.DSVFormat = DepthStencilFormat;

    alphaTestedPsoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
    ThrowIfFailed(device->CreateGraphicsPipelineState(&alphaTestedPsoDesc,IID_PPV_ARGS(&mPso)));
}

void AlphaTestPass::Draw(ID3D12GraphicsCommandList* cmdList, CD3DX12_GPU_DESCRIPTOR_HANDLE hGpuHandler,
    UINT srvHeapSize, UINT objCBByteSize, UINT matCBByteSize, UINT passCBByteSize, 
    ID3D12Resource* objectCB,ID3D12Resource* matCB, ID3D12Resource* passCB,
    const PassConstants& passValue,
    ID3D12Resource* OutColorRenderTarget,D3D12_CPU_DESCRIPTOR_HANDLE OutColorRenderTargetView,D3D12_CPU_DESCRIPTOR_HANDLE OutDepthStentilView
  
    )
{
    cmdList->SetPipelineState(mPso.Get());
    DrawRenderItem(
        cmdList,hGpuHandler,
        srvHeapSize,objCBByteSize,matCBByteSize,
        objectCB,matCB,
        mRenderItems);
}
